<template>
    <div>
     <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
        <!-- 添加用户按钮 -->
        <el-button type="primary">添加角色</el-button>
        <!-- 角色列表区域 -->
        <el-table :data="rolesList" border >
            <!-- 展开列 -->
            <el-table-column type="expand">
                <template slot-scope="scope">   
                    <!-- 布局 -->
                    <el-row :class="['bdbottom',i1===0?'bdtop':'','vcenter']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
                        <!-- 一级菜单区域 -->
                        <el-col :span="5"  >
                            <el-tag @close="removeRightById(scope.row,item1.id)" closable>{{item1.authName}}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <!-- 二级和三级菜单区域 -->
                        <el-col :span="19">
                            <el-row :class="[i2===0?'':'bdtop','vcenter']" v-for="(item2,i2) in item1.children" :key="item2.id">
                                 <!-- 二级权限菜单 -->
                                <el-col :span="6">
                                    <el-tag type="success" @close="removeRightById(scope.row,item2.id)" closable>{{item2.authName}}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <!-- 三级权限菜单 -->
                                <el-col :span="18">
                                    <el-tag v-for="(item3,i3) in item2.children" :key="item3.id" type="warning" closable
                                    @close="removeRightById(scope.row,item3.id)">{{item3.authName}}</el-tag>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                   <!-- <pre>
                       {{ scope.row}}
                   </pre> -->
                </template>
            </el-table-column>
            <!-- 索引列 -->
            <el-table-column type="index"></el-table-column>
            <el-table-column label="角色名称" prop="roleDesc"></el-table-column>
            <el-table-column label="角色描述" prop="roleName"></el-table-column>
            <el-table-column label="操作" width="300px">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                    <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRightDialog(scope.row)">分配权限</el-button>
                    </template>
            </el-table-column>
        </el-table>
    </el-card>
    <!-- 分配权限的对话框 -->
    <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="50%"
        @close="closeSetRightsDialog"
        >
        <!-- 分配权限对话框 -->
        <el-tree :data="rightsList" :props="treeProps" ref="treeRef" show-checkbox node-key="id" default-expand-all
        :default-checked-keys="defKeys">
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setRightDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
    </el-dialog>
    </div>
</template>
<script>
export default {
    data(){
        return {
            rolesList:[],
            setRightDialogVisible:false,
            rightsList:[],
            treeProps:{
                label:'authName',
                children:'children'
            },
            // 默认选中的节点id数组
            defKeys:[105,116],
            // 角色id
            roleId:''
        }
    },
     created(){
        this.getRolesList()
    },
   methods:{
        async getRolesList(){
           const {data:res}=await this.$http.get('roles')
           // console.log(res);
           if(res.meta.status!==200) return this.$message.error('获取角色列表数据失败')
           this.rolesList=res.data
        //    console.log(this.rolesList);
           
        },
        async removeRightById(role,id){
            console.log(role);
            console.log(id);
            const confirmResult=await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err=>err)
            // console.log(confirmResult);
            if(confirmResult!=='confirm') return this.$message.info('用户取消了删除权限')
            // this.$message.info('用户确认了删除权限')
           const {data:res}= await this.$http.delete(`roles/${role.id}/rights/${id}`)
           console.log(res);
            if(res.meta.status!==200) return this.$message.error('删除权限失败')
            // this.getRolesList()
            role.children=res.data
           
        },
        async showSetRightDialog(role){
            // console.log(role);
            this.roleId=role.id;
            console.log( this.roleId);
            
            const {data:res}=await this.$http.get('rights/tree')
            // console.log(res);
            if(res.meta.status!==200) return this.$message.error('获取权限数据失败')
            // this.$message.success('获取权限数据成功')
            this.rightsList=res.data
            // console.log(this.rightsList);
            // 调用getLeafKeys，将三级权限节点的id添加到默认选中的节点id数组
            this.getLeafKeys(role,this.defKeys)
            this.setRightDialogVisible=true
        },
        // 以递归的形式获取三级权限节点
        //参数为节点， 默认选中的节点id数组
        getLeafKeys(node,arr){
            if(!node.children){
                return arr.push(node.id)
            }
            // 递归调用
            node.children.forEach(item=>{
                this. getLeafKeys(item,arr)
            })
        },
      //当用户关闭树形权限对话框的时候，清除掉所有选中状态
        closeSetRightsDialog(){
            this.defKeys=[]
        },
        async allotRights(){
            // 全选的keys
            // const keys=this.$refs.treeRef.getCheckedKeys()
            // 半选的keys
            // const keys1=this.$refs.treeRef.getHalfCheckedKeys()
            // console.log(keys);
            // console.log(keys1);
            const keys=[...this.$refs.treeRef.getCheckedKeys(),
                       ...this.$refs.treeRef.getHalfCheckedKeys()]
            // console.log(keys);
            const rights= keys.join(',')
            console.log(rights);
           const {data:res}=await this.$http.post(`roles/${this.roleId}/rights`,{rids:rights}) 
        //    console.log(res);
        if(res.meta.status!==200) this.$message.error('分配权限失败')
            this.$message.success="分配权限成功"
            // 刷新列表
            this.getRolesList()
            this.setRightDialogVisible = false
        }

   }

}
</script>
<style lang="less" scoped>
.el-tag{
    margin:7px
}
.bdtop{
    border-top:1px solid #eee;
}
.bdbottom{
    border-bottom: 1px solid #eee;
}
// 垂直居中
.vcenter{
    display:flex;
    align-items:center
}
</style>
